using System;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.UI;
using DotNetNuke.UI.Modules;

namespace DnnMvcBridge.Web.Mvc.Base
{
	
	public class ModuleControllerBase : Controller
    {

	    public ModuleInstanceContext ModuleContext { 
			get
			{
				var result = ViewData["ModuleContext"] as ModuleInstanceContext;
				if (result == null)
				{
					throw new NullReferenceException("Unable to get ModuleInstanceContext from ViewData");
				}
				
				return result;
			}
	    }

		public Page Page
		{
			get
			{
				var result = ViewData["Page"] as Page;
				if (result == null)
				{
					throw new NullReferenceException("Unable to get Page from ViewData");
				}

				return result;
			}
		}

	    public ModuleControllerBase()
	    {
		    
	    }

		protected RedirectResult DnnMvcRedirect(int tabId, string controlKey, string actionName)
		{
			return DnnMvcRedirect(tabId, controlKey, new string[]{}, actionName, 0);
		}

		protected RedirectResult DnnMvcRedirect(int tabId, string controlKey, string[] urlParams, string actionName)
		{
			
			return DnnMvcRedirect(tabId, controlKey, urlParams, actionName, 0);
		}

		protected RedirectResult DnnMvcRedirect(int tabId, string controlKey, string actionName, int moduleId)
		{
			return DnnMvcRedirect(tabId, controlKey, new string[]{}, actionName, moduleId);
		}

		protected RedirectResult DnnMvcRedirect(int tabId, string controlKey, string[] urlParams, string actionName, int moduleId)
		{

			if (urlParams != null)
			{
				if (!String.IsNullOrEmpty(actionName))
				{
					Array.Resize(ref urlParams, urlParams.Length + 1);
					urlParams[urlParams.Length - 1] = "action=" + actionName;
				}
			}
			else
			{
				if (!String.IsNullOrEmpty(actionName))
				{
					urlParams = new string[] { "action=" + actionName };
				}
			}

			return DnnMvcRedirect(tabId, controlKey, urlParams, moduleId);
			
		}

		protected RedirectResult DnnMvcRedirect(int tabId, string controlKey, int moduleId)
		{
			return DnnMvcRedirect(tabId, controlKey, new string[] {}, moduleId);
		}

		protected RedirectResult DnnMvcRedirect(int tabId, string controlKey, string[] urlParams, int moduleId)
		{
		
			if (moduleId > 0)
			{

				if (urlParams != null)
				{
					Array.Resize(ref urlParams, urlParams.Length + 1);
					urlParams[urlParams.Length - 1] = "mid=" + moduleId;

				}
				else
				{
					urlParams = new string[] { "mid=" + moduleId };
				}

			}
		
			return DnnMvcRedirect(tabId, controlKey, urlParams);

		}
		
		protected RedirectResult DnnMvcRedirect(int tabId, string controlKey, string[] urlParams)
		{

			string url = ModuleContext.NavigateUrl(tabId, controlKey, false, urlParams);

			return base.Redirect(url);

		}


		/// <summary>
		/// Redirect is supported in DnnMvcBridge but DnnMvcRedirect would be a better option for navigation within Dnn.
		/// </summary>
		/// <param name="url"></param>
		/// <returns></returns>
		protected override RedirectResult Redirect(string url)
		{
			return base.Redirect(url);
		}
		
		#region Not Supported
		protected override RedirectResult RedirectPermanent(string url)
		{
			throw new NotImplementedException("RedirectPermanent is not supported in DNN modules using DnnMvcBridge");
		}

		protected override RedirectToRouteResult RedirectToAction(string actionName, string controllerName,RouteValueDictionary routeValues)
		{
			throw new NotImplementedException("RedirectToAction is not supported in DNN modules using DnnMvcBridge");
		}

		protected override RedirectToRouteResult RedirectToActionPermanent(string actionName, string controllerName, RouteValueDictionary routeValues)
		{
			throw new NotImplementedException("RedirectToActionPermanent is not supported in DNN modules using DnnMvcBridge");
		}

		protected override RedirectToRouteResult RedirectToRoute(string routeName,RouteValueDictionary routeValues)
		{
			throw new NotImplementedException("RedirectToRoute is not supported in DNN modules using DnnMvcBridge");
		}

		protected override RedirectToRouteResult RedirectToRoutePermanent(string routeName,RouteValueDictionary routeValues)
		{
			throw new NotImplementedException("RedirectToRoutePermanent is not supported in DNN modules using DnnMvcBridge");
		}

		#endregion

    }
}
